package com.enjoy.xbase.ui.load;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.LayoutRes;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.enjoy.xbase.R;

/**
 * 描述：
 *  默认加载视图
 * 介绍：
 *  1、继承来自相对布局
 *  2、数据展示层与加载处于同一级、也可将数据层放入加载层内
 *  3、加载过程场景视图可通过xml、代码set、代码设置回传接口三种方式实现
 *   已过时 采用LoadView替代
 */
@Deprecated
public class DefaultLoadView extends BaseLoadView {

    @LayoutRes
    private int loadLayoutId = R.layout.xbase_lv_default_load;
    @LayoutRes
    private int emptyLayoutId = R.layout.xbase_lv_default_empty;
    @LayoutRes
    private int faillLayoutId = R.layout.xbase_lv_default_faill;
    @LayoutRes
    private int loadingLayoutId = R.layout.xbase_lv_default_loading;

    private int backgroundColorType=1;//判断背景颜色类型 1-默认值 2-设置值
    private int backgroundColor = Color.parseColor("#ffffff");

    /*界面交互器*/
    private LoadMutual loadMutual;
    /*数据刷新接口*/
    private ILoadRefresh loadRefresh;


    public DefaultLoadView(Context context) {
        super(context);

    }

    public DefaultLoadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public DefaultLoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadView);
        loadLayoutId = a.getResourceId(R.styleable.LoadView_load, R.layout.xbase_lv_default_load);
        emptyLayoutId = a.getResourceId(R.styleable.LoadView_empty, R.layout.xbase_lv_default_empty);
        faillLayoutId = a.getResourceId(R.styleable.LoadView_faill, R.layout.xbase_lv_default_faill);
        loadingLayoutId = a.getResourceId(R.styleable.LoadView_loading, R.layout.xbase_lv_default_loading);
        if (a.hasValue(R.styleable.LoadView_bgColor)){//判断是否设置
            backgroundColor = a.getColor(R.styleable.LoadView_bgColor, Color.parseColor("#ffffff"));
            this.backgroundColorType=2;
        }
    }
    public void setLoadBackgroundColor(int color) {
        this.backgroundColor = color;
        this.backgroundColorType=2;
    }


    /**
     * 设置界面交互器
     * @param loadMutual
     */
    public void setLoadMutual(LoadMutual loadMutual) {
        this.loadMutual = loadMutual;
    }

    /**
     * 描述设置界面刷新接口
     * @param loadRefresh
     */
    public void setLoadRefresh(ILoadRefresh loadRefresh) {
        this.loadRefresh = loadRefresh;
    }

    /**
     * 描述：设置默认的刷新事件
     */
    private void  setDefaultBtnRefreshListener(View view){
        View btnRefresh = view.findViewById(R.id.load_btn_refresh);
        if (btnRefresh != null) {
            btnRefresh.setOnClickListener((v) -> {
                if (loadRefresh != null) loadRefresh.onRefresh();
            });
        }
        View btnSrlRefresh = view.findViewById(R.id.load_srl_refresh);
        if (btnSrlRefresh != null && btnSrlRefresh instanceof SwipeRefreshLayout) {
            ((SwipeRefreshLayout) btnSrlRefresh).setOnRefreshListener(() -> {
                ((SwipeRefreshLayout) btnSrlRefresh).setRefreshing(false);
                if (loadRefresh != null) {
                    loadRefresh.onRefresh();
                }
            });
        }
    }
    @Override
    public View createLoadView(ViewGroup parent) {
        View view;
        if (loadMutual != null) {
            view = loadMutual.createLoadView(parent);
            if (view != null) return view;
            if (loadMutual.getLoadLayoutId() > 0) {
                view = LayoutInflater.from(getContext()).inflate(loadMutual.getLoadLayoutId(), parent, false);
                return view;
            }
        }
        if (getLoadLayoutId()>0){
            view = LayoutInflater.from(getContext()).inflate(getLoadLayoutId(), parent, false);
        } else {
            view = LayoutInflater.from(getContext()).inflate(loadLayoutId, parent, false);
        }
        if (backgroundColorType==2){
            view.setBackgroundColor(backgroundColor);
        }
        return view;
    }

    @Override
    public View createFaillView(ViewGroup parent) {
        View view;
        if (loadMutual != null) {
            view = loadMutual.createFaillView(parent);
            if (view != null) return view;
            if (loadMutual.getFaillLayoutId() > 0) {
                view = LayoutInflater.from(getContext()).inflate(loadMutual.getFaillLayoutId(), parent, false);
                setDefaultBtnRefreshListener(view);
                return view;
            }
        }
        if (getFaillLayoutId()>0){
            view = LayoutInflater.from(getContext()).inflate(getFaillLayoutId(), parent, false);
        } else {
            view = LayoutInflater.from(getContext()).inflate(faillLayoutId, parent, false);
        }
        if (backgroundColorType==2){
            view.setBackgroundColor(backgroundColor);
        }
        setDefaultBtnRefreshListener(view);
        return view;
    }

    @Override
    public View createEmptyView(ViewGroup parent) {
        View view;
        if (loadMutual != null) {
            view = loadMutual.createEmptyView(parent);
            if (view != null) return view;
            if (loadMutual.getEmptyLayoutId() > 0) {
                view = LayoutInflater.from(getContext()).inflate(loadMutual.getEmptyLayoutId(), parent, false);
                setDefaultBtnRefreshListener(view);
                return view;
            }
        }
        if (getEmptyLayoutId()>0){
            view = LayoutInflater.from(getContext()).inflate(getEmptyLayoutId(), parent, false);
        } else {
            view = LayoutInflater.from(getContext()).inflate(emptyLayoutId, parent, false);
        }
        if (backgroundColorType==2){
            view.setBackgroundColor(backgroundColor);
        }
        setDefaultBtnRefreshListener(view);
        return view;
    }

    @Override
    public View createLoadingView(ViewGroup parent) {
        View view;
        if (loadMutual != null) {
            view = loadMutual.createLoadingView(parent);
            if (view != null) return view;
            if (loadMutual.getLoadingLayoutId() > 0) {
                view = LayoutInflater.from(getContext()).inflate(loadMutual.getLoadingLayoutId(), parent, false);
                return view;
            }
        }
        if (getLoadingLayoutId()>0){
            view = LayoutInflater.from(getContext()).inflate(getLoadingLayoutId(), parent, false);
        } else {
            view = LayoutInflater.from(getContext()).inflate(loadingLayoutId, parent, false);
        }
        return view;
    }

    @Override
    protected int getLoadLayoutId() {
        return -1;
    }

    @Override
    protected int getFaillLayoutId() {
        return -1;
    }

    @Override
    protected int getEmptyLayoutId() {
        return -1;
    }

    @Override
    protected int getLoadingLayoutId() {
        return -1;
    }

    @Override
    public void onLoadActionStart() {
        if (loadMutual != null) {
            loadMutual.onLoadActionStart();
        }
    }

    @Override
    public void onLoadActionEnd() {
        if (loadMutual != null) {
            loadMutual.onLoadActionEnd();
        }
    }

    @Override
    public void onLoadingActionStart() {
        if (loadMutual != null) {
            loadMutual.onLoadingActionStart();
        }
    }

    @Override
    public void onLoadingActionEnd() {
        if (loadMutual != null) {
            loadMutual.onLoadingActionEnd();
        }
    }
}
